package com.ftw.commons

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.appcompat.app.AppCompatDialogFragment
import com.ftw.commons.utils.ShapeUtils
import kotlinx.android.synthetic.main.dialog_center_base.*


/**
 * @Description: 弹窗基类
 * @Author: fangtianwen
 * @Date: 2020/1/13 17:55
 */
abstract class IBaseDialogFragment(var mContext: Context) : AppCompatDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.dialog_center_base, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        val view1 = LayoutInflater.from(mContext).inflate(getViewId(), null, false)
        ll_content.addView(view1)
        ShapeUtils.setRadius(ll_content, getRadius())

        dialog?.let {
            it.requestWindowFeature(Window.FEATURE_NO_TITLE)
            it.setCancelable(isCancelable)
            it.setCanceledOnTouchOutside(isCancelableOutside())
            it.setOnKeyListener { dialog, keyCode, event -> keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN && !isCancelable }
        }



        initView(view)
    }

    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return
        //设置背景色透明
        //设置背景色透明
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        //设置Dialog动画效果
        //设置Dialog动画效果
        if (getAnimRes() > 0) {
            window.setWindowAnimations(getAnimRes())
        }
        val params = window.attributes
        //设置Dialog的Width
        //设置Dialog的Width
        params.width = getDialogWidth()
        //设置Dialog的Height
        //设置Dialog的Height
        if (getDialogHeight() > 0) {
            params.height = getDialogHeight()
        } else {
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
        }
        //设置屏幕透明度 0.0f~1.0f(完全透明~完全不透明)
        //设置屏幕透明度 0.0f~1.0f(完全透明~完全不透明)
        params.dimAmount = getDimAmount()
        params.gravity = showLocation()
        window.attributes = params
    }


    protected open fun isCancelableOutside(): Boolean {
        return true
    }

    protected open fun getAnimRes(): Int {
        return 0
    }

    protected open fun getDialogWidth(): Int {
        return WindowManager.LayoutParams.WRAP_CONTENT
    }

    protected open fun getDialogHeight(): Int {
        return WindowManager.LayoutParams.WRAP_CONTENT
    }

    open fun getDimAmount(): Float {
        return 0.2f
    }

    protected open fun getGravity(): Int {
        return Gravity.CENTER
    }


    override fun onResume() {
        super.onResume()
        getWidth()
    }

    open fun getWidth() {}

    /**
     * 子布局
     */
    abstract fun getViewId(): Int

    /**
     * 提供子布局统一初始化方法
     */
    abstract fun initView(view: View)

    /**
     * 显示位置
     */
    abstract fun showLocation(): Int

    /**
     * 显示弹窗的四个圆角度数 ，默认为哦
     */
    abstract fun getRadius(): DialogRadius

}

class DialogRadius(
    var topLeft: Float = 0f,
    val bottomLeft: Float = 0f,
    val topRight: Float = 0f,
    val bottomRight: Float = 0f
)
